﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Thor.Configuration;
using Thor.Domain;
using Thor.Domain.Configuration;

namespace Thor.Domain
{
    public class InvalidLogonMonitorHelper
    {
        #region fields

        private static ConfigurationSettings _ConfigSettings = new ConfigurationSettings();
        private static int _interval = Convert.ToInt32(ConfigurationSettings.InvalidLogonMonitorInterval);
        private static InvalidLogonMonitor _monitor = null;
        private static object _lockObject = new Object();

        #endregion

        #region constructors

        private InvalidLogonMonitorHelper() { }

        #endregion

        #region properties

        private static ConfigurationSettings ConfigurationSettings
        {
            get
            {
                if (_ConfigSettings == null)
                    _ConfigSettings = new ConfigurationSettings();

                return _ConfigSettings;
            }
        }

        public static bool IsRunning { get; internal set; }

        #endregion

        #region methods

        /// <summary>
        /// Starts monitoring.
        /// </summary>
        public static void StartMonitor()
        {
            if (!IsRunning)
            {
                var settings = SettingsHelper.GetSettings();
                var repository = InvalidLogonHelper.Repository;
                var monitor = GetInvalidLogonMonitor(settings, repository);

                monitor.Start();

                IsRunning = true;
            }
        }

        /// <summary>
        /// Stops monitoring.
        /// </summary>
        public static void StopMonitor()
        {
            if (IsRunning)
            {
                if (_monitor != null)
                    _monitor.Stop();

                IsRunning = false;
            }
        }

        /// <summary>
        /// Restarts monitoring.
        /// </summary>
        public static void RestartMonitor()
        {
            if (IsRunning)
            {
                StopMonitor();
                //TODO: wait until all monitor is stopped

                StartMonitor();
                //TODO: wait until all monitor is started

            }
        }

        /// <summary>
        /// Gets the monitor for invalid logons.
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        private static InvalidLogonMonitor GetInvalidLogonMonitor(Settings settings, IInvalidLogonRepository repository)
        {
            InvalidLogonMonitorParameters parameters = new InvalidLogonMonitorParameters();
            parameters.Interval = _interval;
            parameters.LoadOnStart = true;
            parameters.Repository = repository;
            parameters.Settings = settings;

            return new InvalidLogonMonitor(parameters);
        }

        #endregion
    }
}
